package com.wh.wisdomsite.xbox.server;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wh.wisdomsite.xbox.ba.entity.ServiceMethod;
import com.wh.wisdomsite.xbox.ba.service.IServiceMethodService;
import com.wh.wisdomsite.xbox.pojo.ba.ServiceMethodIn;
import com.wh.wisdomsite.xbox.common.util.ResManager;
import java.util.*;
import com.wh.wisdomsite.xbox.utils.date.DateTool;
import com.wh.wisdomsite.xbox.utils.json.JsonUtil_Org;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 功能描述：服务配置管理控制器Controller
 * @Package com.wh.wisdomsite.xbox.server
 * @author 谢泽鹏
 * @date 2014年10月8日 下午4:02:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 * @version V1.0
 */
@Controller
@RequestMapping(value = "/serviceMethod")
public class ServiceMethodController {

    private static final Log logger = LogFactory.getLog(ServiceMethodController.class);

    @Autowired
    private IServiceMethodService iServiceMethodService;


    /**
     * 页面跳转
     * @return
     */
    @RequiresPermissions("rules:dataService_serviceMehod_page")
    @RequestMapping(value = "index.do", method = RequestMethod.GET)
    public String index(){
        return "/dataServices/serviceMethod/serviceMethodList";
    }

    /**
     * 获取服务配置集合
     * @param response
     * @param in
     */
    @RequiresPermissions("rules:dataService_serviceMehod_find")
    @RequestMapping(value = "/serviceMethodList.do")
    public void serviceMethodList(HttpServletResponse response, ServiceMethodIn in){
        try {
            logger.info("加载服务配置数据列表-->>开始");
            //设置分页
            if(in.getPage() == 0) in.setPage(1);
            if(in.getRows() == 0) in.setRows(10);
            Page<ServiceMethod> page = new Page<ServiceMethod>();
            page.setCurrent(in.getPage());
            page.setSize(in.getRows());
            //设置条件
            EntityWrapper<ServiceMethod> ewServiceMethod = new EntityWrapper<ServiceMethod>();
            ewServiceMethod.setEntity(new ServiceMethod());
            if(StringUtils.isNotEmpty(in.getMethod())){
                ewServiceMethod.like("T_METHOD", in.getMethod());
            }
            if(StringUtils.isNotEmpty(in.getTitle())){
                ewServiceMethod.like("T_TITLE", in.getTitle());
            }
            ewServiceMethod.orderBy("T_METHOD, T_CREATE_DATE", true);
            Page<ServiceMethod> serviceMethodPage = iServiceMethodService.selectPage(page, ewServiceMethod);
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();
            if(serviceMethodPage.getRecords().size() > 0){
                for(ServiceMethod serviceMethod : serviceMethodPage.getRecords()){
                    JSONObject object = new JSONObject(serviceMethod);
                    object.put("id",serviceMethod.getId());
                    object.put("createDate", DateTool.format(serviceMethod.getCreateDate(), "yyyy-MM-dd HH:mm:ss"));
                    array.put(object);
                }
            }
            json.put("total",serviceMethodPage.getTotal());
            json.put("rows",array);
            ResManager.getTextOut(response).print(json);
            logger.info("加载服务配置数据列表-->>结束");
        }catch (Exception e){
            e.printStackTrace();
            logger.debug("加载服务配置数据列表-->>Exception" + e.getMessage());
        }
    }

    /**
     * 服务配置详情
     * @param serviceMethodIn
     * @param request
     * @return
     */
    @RequiresPermissions("rules:dataService_serviceMehod_view")
    @RequestMapping("serviceMethodDesc.do")
    public String appSecretDesc(ServiceMethodIn serviceMethodIn, HttpServletRequest request) {
        logger.info("服务配置详情-->>开始");
        ServiceMethod serviceMethod = iServiceMethodService.selectById(serviceMethodIn.getId());
        request.setAttribute("serviceMethod", serviceMethod);
        request.setAttribute("createDate", DateTool.format(serviceMethod.getCreateDate(), "yyyy-MM-dd HH:mm:ss"));
        request.setAttribute("updateDate", DateTool.format(serviceMethod.getUpdateDate(), "yyyy-MM-dd HH:mm:ss"));
        logger.info("服务配置详情-->>结束");
        return "/dataServices/serviceMethod/serviceMethodDesc";
    }

    /**
     * 更新或者修改
     * @param serviceMethod
     * @param response
     */
    @RequiresPermissions("rules:dataService_serviceMehod_edit")
    @RequestMapping(value = "/save.do", method = RequestMethod.POST)
    public void save(ServiceMethod serviceMethod,HttpServletResponse response){
        try {
            //判断方法是否已经存在,存在即更新.否则为添加
            if(StringUtils.isNotEmpty(serviceMethod.getId())){
                logger.info("修改服务配置-->>开始");
                serviceMethod.setUpdateDate(new Date());
                iServiceMethodService.updateById(serviceMethod);
                logger.info("修改服务配置-->>结束");
                ResManager.getJsonOut(response).print(JsonUtil_Org.returnTrue("服务配置更新成功"));
            }else {
                logger.info("添加服务配置-->>开始");
                serviceMethod.setCreateDate(new Date());
                serviceMethod.setId(UUID.randomUUID().toString().replace("-",""));
                iServiceMethodService.insert(serviceMethod);
                logger.info("添加服务配置-->>结束");
                ResManager.getJsonOut(response).print(JsonUtil_Org.returnTrue("服务配置添加成功"));
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.debug("操作服务配置-->>Exception" + e.getMessage());
        }
    }

    /**
     * 删除服务配置
     * @param id
     * @param response
     */
    @RequiresPermissions("rules:dataService_serviceMehod_del")
    @RequestMapping(value = "/del.do", method = RequestMethod.GET)
    public void del(String id, HttpServletResponse response){
        try{
            logger.info("删除服务配置-->>开始");
            iServiceMethodService.deleteById(id);
            logger.info("删除服务配置-->>结束");
            ResManager.getJsonOut(response).print(JsonUtil_Org.returnTrue("服务配置删除成功"));
        } catch (Exception e){
            e.printStackTrace();
            logger.error("删除服务配置-->>Exception" + e.getMessage());
        }
    }
}
